<template>
  <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false"
             v-dialogDrag>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="工序编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入工序编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工序名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入工序名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品型号" prop="goodsModel">
        <el-input v-model="queryParams.goodsModel" placeholder="请输入产品型号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="图纸号" prop="shuoming">
        <el-input v-model="queryParams.shuoming" placeholder="请输入图纸号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="margin-left: 10px;">
      <el-form ref="form" :rules="rules" label-width="120px">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                     v-hasPermi="['develop:LnGongxu:add']">新增</el-button>
        </el-col>
        <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading" @selection-change="handleSelectionChange" @sort-change="handleSortChange" :max-height="tableMaxHeight">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column sortable label="工序编号" align="center" prop="code"/>
          <el-table-column sortable label="工序名称" align="center" prop="name"/>
          <el-table-column sortable label="产品名称" align="center" prop="goodsName"/>
          <el-table-column sortable label="产品型号" align="center" prop="goodsModel"/>
          <!--<el-table-column sortable label="计件工资" align="center" prop="money"/>
          <el-table-column label="关联设备" align="center" prop="photo"/>-->
          <el-table-column label="使用车间id" align="center" prop="deptId"/>
          <el-table-column label="使用车间" align="center" prop="deptName"/>
          <el-table-column label="标准工时" align="center" prop="workHour"/>
          <el-table-column label="图纸号" align="center" prop="shuoming" show-overflow-tooltip/>
          <el-table-column align="center" label="工序文件" prop="file" width="200">
            <template slot-scope="scope">
              <image-preview :src="scope.row.file || ''" height="60px" width="40px"></image-preview>
            </template>
          </el-table-column>
          <!--<el-table-column align="center" label="工序图纸" prop="photo" width="200">
            <template slot-scope="scope">
              <image-preview :src="scope.row.photo" height="60px" width="40px"></image-preview>
            </template>
          </el-table-column>-->
          <el-table-column label="备注" align="center" prop="remark"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit"
                         @click="handleUpdate(scope.row)" v-hasPermi="['develop:LnGongxu:edit']">修改</el-button>
              <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                             style="margin-left: 10px;">
                <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                           v-hasPermi="['develop:LnGongxu:remove']">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
      <lnGongxu-form ref="form" @ok="getList"></lnGongxu-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary"  @click="submitGxGyGoods">确 认 选 择</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getAction,addAction,deleteAction} from '@/api/manage'
  import LnGongxuForm from '@/views/develop/lnGongxu/module/LnGongxuForm'

  export default {
    name: "GoodsGongxuAddForm",
    components: {
      LnGongxuForm,
    },
    data() {
      return {
        title: "工序1列表",
        open: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        rows: [],
        rules: {
        },
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 销售信息管理表格数据
        dataList: [],
        columns: [],
        params: {},
        goodsCount: null,
        saleId: null,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          shuoming: null,
          gongyiId: null,
          gongxuId: null,
          goodsName:null,
          goodsModel:null,
          code: null,
          name: null,
          deptId: null,
          deptName: null,
          workHour: null,
          orderByColumn: "code",
          isAsc: "asc",
          params: {},
        },
        url: {
          list:'/lnGongxu/lnGongxu/list',
          addGoodsDoGongxu:'/goodsSaledoGongxu/goodsSaledoGongxu/addGoodsDoGongxu',
          remove:'/lnGongxu/lnGongxu/',
        },
        tableMaxHeight: window.innerHeight - 420,
      };
    },
    created() {
      // this.intDictsForPrj();
      window.addEventListener('resize', this.getTableHeight);
    },
    destroyed() {
      window.removeEventListener('resize', this.getTableHeight);
    },
    methods: {
      handleAdd() {
        this.$refs.form.handleAdd();
      },
      /** 排序触发事件 */
      handleSortChange(column, prop, order) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
        this.getList();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$refs.form.handleUpdate(row);
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        deleteAction(this.url.remove, ids).then(response => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      getTableHeight() {
        this.tableHeight = window.innerHeight - 320
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      handleAddGoodsGongxu(saleId,goodsCount){
        this.goodsCount=goodsCount;
        this.saleId=saleId;
        this.open=true;
        this.getList();
      },
      /** 查询销售信息管理列表 */
      getList() {
        this.loading = true;
        getAction(this.url.list, this.queryParams).then(response => {
          this.columns = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      submitGxGyGoods(){
        if (this.ids == null || this.ids == '' || this.ids == 'undefined') {
          this.$modal.msgError("请选择一条记录");
        } else {
          //let params ={"ids":this.ids.toString(),"goodsCount":this.goodsCount,"saleId":this.saleId}
          this.params.ids=this.ids;
          this.params.goodsCount=this.goodsCount;
          this.params.saleId=this.saleId;
          addAction(this.url.addGoodsDoGongxu, this.params).then(response => {
            this.$modal.msgSuccess("添加完成");
            this.open = false;
            this.$emit('ok');
          });
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
    }
  };
</script>
